//
// Created by xili on 2024/9/4 20:59.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    bool isNStraightHand(vector<int> &hand, int groupSize) {
        map<int, int> ump;
        int n = hand.size();
        if (n % groupSize != 0) {
            return false;
        }
        int group = n / groupSize;
        for (auto &it: hand) {
            ump[it]++;
        }
        for (int i = 0; i < group; i++) {
            int start = ump.begin()->first;
            for (int j = 0; j < groupSize; j++) {
                if (!ump.count(start + j)) {
                    return false;
                }
                ump[start + j]--;
                if (ump[start + j] == 0) {
                    ump.erase(start + j);
                }
            }
        }
        return true;
    }
};